package com.czy.owner.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.CollectionProductModel;
import com.czy.owner.entity.UserCollectInfoModel;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.List;


/**
 * Created by wangyang on 2017/5/14 0014.
 */

public class UserCollectProductAdapter extends BaseRecyclerAdapter<CollectionProductModel.ListBean, UserCollectProductAdapter.ColectProductHolder> {
    private Context context;
    private DeletedListener deleteListener;
    public UserCollectProductAdapter(Context context, List<CollectionProductModel.ListBean> data, boolean slideEnabled, boolean reboundEnabled) {
        super(context, data, slideEnabled, reboundEnabled);
        this.context = context;
    }

    @Override
    public ColectProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_collection_product, parent, false);
        return new ColectProductHolder(view);
    }

    @Override
    public void onBindViewHolder(final ColectProductHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
        holder.tvItemDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteListener != null) {
                    deleteListener.onClick(view, position,data);
                }
            }
        });


    }

    public class ColectProductHolder extends BaseRecyclerAdapter.ViewHolder {
        private ImageView imgProduct;
        private TextView tvProductDescription;
        private TextView tvProductPrice;
        private LinearLayout slideButtonLayout;
        private TextView tvItemDeleteBtn;
        private TextView tvMemberPrice;

        public ColectProductHolder(View itemView) {
            super(itemView);
            imgProduct = (ImageView) itemView.findViewById(R.id.img_product);
            tvProductDescription = (TextView) itemView.findViewById(R.id.tv_product_description);
            tvProductPrice = (TextView) itemView.findViewById(R.id.tv_product_price);
            slideButtonLayout = (LinearLayout) itemView.findViewById(R.id.slide_button_layout);
            slideButtonLayout = (LinearLayout) itemView.findViewById(R.id.slide_button_layout);
            tvItemDeleteBtn = (TextView) itemView.findViewById(R.id.tv_item_delete_btn);
            tvMemberPrice = (TextView) itemView.findViewById(R.id.tv_member_price);

        }

        public void bindData(CollectionProductModel.ListBean data) {
            slideButtonLayout.setVisibility(View.VISIBLE);
            tvProductDescription.setText(data.getGoodsName());
            tvProductPrice.setText("￥"+String.format("%.2f",data.getPrice()));
            if(data.getHaveMemberPriceFlag().equals("true")){
                tvMemberPrice.setVisibility(View.VISIBLE);
                tvMemberPrice.setText("￥"+String.format("%.2f",data.getStoreMemberPrice()));
            }else {
                tvMemberPrice.setVisibility(View.GONE);
            }
            GlideUtils.loadImage(context, data.getGoodsLogo(), imgProduct, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_banner, R.mipmap.default_banner);

        }
    }

    public interface DeletedListener {
        void onClick(View v, int position, List<CollectionProductModel.ListBean> data);
    }

    public void setDeleteListener(DeletedListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public void removeData(int position) {
        data.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, data.size() - position);
    }



}